﻿
//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////

#include "GraphicsStyleDlg.h"
#include "IUiView.h"
#include "ICategory.h"
#include "IDocument.h"
#include "IUiDocument.h"
#include "UniIdentity.h"
#include "JournalUtils.h"
#include "UiCommonDialog.h"
#include "IPublishConfig.h"
#include "IJournalUiEvent.h"
#include "ICategoryLibrary.h"
#include "IGraphicsStyleManager.h"
#include "ICategoryAndGraphicsStyleFilter.h"
#include "GcmpBuiltInCategoryUniIdentities.h"

#include "qevent.h"
#include "GmTitleBar.h"
#include "QDialogButtonBox.h"
#include "GStyleViewerUtils.h"
#include "GmStyleSheetHelper.h"
#include "GraphicsStyleTable.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;
using namespace Sample;

GraphicsStyleDlg::GraphicsStyleDlg(gcmp::IUiDocument* pUIDoc, gcmp::IUiView* currentView,
    std::wstring& title, std::vector<gcmp::OwnerPtr<Sample::GraphicsOverrideData>>& gOverrideDataList)
    : QDialog(nullptr, Qt::FramelessWindowHint | Qt::WindowSystemMenuHint
        | Qt::WindowMinMaxButtonsHint)
    , m_wpUIDoc(pUIDoc)
    , m_wpCurrentView(currentView)
    , m_opApplyBntMouseEvent(nullptr)
    , m_gOverrideDataList(gOverrideDataList)
{
    m_ui.setupUi(this);
    resize(1538, 500);
    DBG_WARN_AND_RETURN_VOID_UNLESS(pUIDoc, L"无效参数",L"GDMP",L"2024-03-30");
    gcmp::IDocument* pDoc = pUIDoc->GetDbDocument();
    DBG_WARN_AND_RETURN_VOID_UNLESS(pDoc, L"doc 为空",L"GDMP",L"2024-03-30");
    DBG_WARN_AND_RETURN_VOID_UNLESS(gOverrideDataList.size() == 9, L"gOverrideDataList个数不是期望的9个",L"GDMP",L"2024-03-30");

    // 设置对话框样式
    std::wstring styleSheetStr = GmStyleSheetHelper::Instance()->GetCurrentStyleData();
    setStyleSheet(QString::fromStdWString(styleSheetStr));
    m_ui.gbmp_title->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
    m_ui.gbmp_title->setIconVisible(false);
    m_ui.gbmp_title->setMaxButtonVisible(false);
    m_ui.gbmp_title->setMinButtonVisible(false);
    m_ui.gbmp_title->setText(QString::fromStdWString(title));
    this->setProperty("GStyle_Title", true);
    connect(m_ui.gbmp_title, &GmTitleBar::closeRequested, this, &GraphicsStyleDlg::reject);
    connect(m_ui.gbmp_title, &GmTitleBar::moveRequested, this, &GraphicsStyleDlg::onMoveRequested);

    // 响应按钮
    DBG_WARN_AND_RETURN_VOID_UNLESS(m_ui.applyBnt != nullptr, L"m_ui.applyBnt 为空",L"GDMP",L"2024-03-30");
    m_ui.applyBnt->button(QDialogButtonBox::Ok)->setObjectName(QStringLiteral("buttonOK"));
    m_ui.applyBnt->button(QDialogButtonBox::Cancel)->setObjectName(QStringLiteral("buttonCancel"));
    m_ui.applyBnt->button(QDialogButtonBox::Ok)->setText(QString::fromStdWString(GBMP_TR(L"确定")));
    m_ui.applyBnt->button(QDialogButtonBox::Cancel)->setText(QString::fromStdWString(GBMP_TR(L"取消")));

    QObject::connect(m_ui.applyBnt, SIGNAL(accepted()), this, SLOT(OnApply()));

    // 初始化【投影和截面线样式】页面
    {
        QWidget* pTabPage = NEW_AS_QT_CHILD(QWidget, m_ui.tabWidget);
        OwnerPtr<GraphicsStyleTable> opTableLines =
            NEW_AS_OWNER_PTR(GraphicsStyleTable, pUIDoc, currentView, this, GraphicsStyleTable::TableType::ProjectionSectionLines, m_gOverrideDataList);
        DBG_WARN_AND_RETURN_VOID_UNLESS(opTableLines, L"opTableLines为空",L"GDMP",L"2024-03-30");
        QVBoxLayout* pVLayout = NEW_AS_QT_CHILD(QVBoxLayout, pTabPage);
        pVLayout->addWidget(opTableLines->GetWidgetFW());
        opTableLines->GetWidgetFW()->setParent(pTabPage);
        m_ui.tabWidget->addTab(pTabPage, QString::fromStdWString(L"投影和截面线样式"));
        m_opTables.push_back(TransferOwnership(opTableLines));
    }
    // 初始化【投影和截面面样式】页面
    {
        QWidget* pTabPage = NEW_AS_QT_CHILD(QWidget, m_ui.tabWidget);
        OwnerPtr<GraphicsStyleTable> opTableFaces =
            NEW_AS_OWNER_PTR(GraphicsStyleTable, pUIDoc, currentView, this, GraphicsStyleTable::TableType::ProjectionSectionFaces, m_gOverrideDataList);
        DBG_WARN_AND_RETURN_VOID_UNLESS(opTableFaces, L"opTableFaces为空",L"GDMP",L"2024-03-30");
        QVBoxLayout* pVLayout = NEW_AS_QT_CHILD(QVBoxLayout, pTabPage);
        pVLayout->addWidget(opTableFaces->GetWidgetFW());
        opTableFaces->GetWidgetFW()->setParent(pTabPage);
        m_ui.tabWidget->addTab(pTabPage, QString::fromStdWString(L"投影和截面面样式"));
        m_opTables.push_back(TransferOwnership(opTableFaces));
    }
    m_ui.tabWidget->removeTab(0);
}

GraphicsStyleDlg::~GraphicsStyleDlg()
{
}

void GraphicsStyleDlg::OnApply()
{
    for (int i = 0; i < (int)m_opTables.size(); ++i)
    {
        if (m_opTables[i] == nullptr)
        {
            continue;
        }
        m_opTables[i]->OnApplied();
    }
}

void GraphicsStyleDlg::onMoveRequested(const QPoint& cursorPos, const QPoint& offset)
{
    Q_UNUSED(cursorPos);
    move(pos() + offset);
}

